﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace DomainModel.Entities
{
    // This is the Linq to SQL class for the Answers table.
    // This is the bottom layer of the DAL.
    [Table(Name = "Answers")]
    public class Answer : IDataErrorInfo
    {
        [Column(Name = "answerID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int AnswerID { get; set; }

        [Column(Name = "questionID")]
        public int QuestionID {get; set;}

        [Column(Name = "answer")]
        public string QAnswer { get; set; }
        [Column(Name = "value")]
        public int Value { get; set; }

        [Association(OtherKey = "AnswerID")]
        private EntitySet<AnsweredQuestion> _answeredQuestions = new EntitySet<AnsweredQuestion>();
        public IList<AnsweredQuestion> AnsweredQuestions { get { return _answeredQuestions.ToList().AsReadOnly(); } }

        public string this[string fieldName]
        {
            get
            {
                if (((fieldName == "QAnswer") && string.IsNullOrEmpty(QAnswer)) || 
                    ((fieldName == "QAnswer") && (QAnswer.Length > 8000)))
                    return "Answer must not be empty or longer than 8000 characters.";

                if (((fieldName == "Value") && (Value > 5)) ||
                    ((fieldName == "Value") && (Value < -5)))
                    return "Value must be between -5 and 5.";

                return null;
            }
        }

        public string Error { get { return null; } }
    }
}